#include <57header.h>

int main(int argc,char*argv[])
{
    char *port = "8080";
    char *ip = "192.168.106.129";


    int net_fd = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    connect(net_fd, (struct sockaddr *)&addr, sizeof(addr));


    int name_len = 0;
    recv(net_fd, &name_len, sizeof(int), MSG_WAITALL);

    char fileName[60] = {0};
    recv(net_fd, fileName, name_len, MSG_WAITALL);
    printf("fileName: %s \n", fileName);

    int file_fd = open(fileName, O_RDWR|O_CREAT, 0600);


    while(1){
        ssize_t file_len =0;
        int res = recv(net_fd, &file_len, sizeof(ssize_t), MSG_WAITALL);
        printf("file_len: %ld \n", file_len);
        if(res == 0){
            // 对方关闭链接, 数据发完了
            break;
        }
        char buf[1024] = {0};
        res = recv(net_fd, buf, file_len, MSG_WAITALL);
        if(res == 0){
            // 对方关闭链接, 数据发完了
            break;
        }

        write(file_fd, buf, strlen(buf));

    }

    close(file_fd);

    close(net_fd);
    return 0;
}

